<template>
  <div class="mycourse">
    <div
      class="underline"
      style="width:120px;"
    >
      <span class="title1 color1 zIndex fontBold">我的课程</span>
    </div>
    <div class="mycourseBox">
      <van-pull-refresh
        v-model="refreshing"
        disabled
        @refresh="onRefresh"
        class="refresh-content"
      >
        <van-list
          class="content-list"
          v-model:loading="loading"
          :finished="finished"
          @load="loadMore"
          :immediate-check="false"
        >
          <div
            class="college_list"
            v-for="(item, index) in demandList"
            :key="index"
            @click="goCurriculum(item.id)"
          >
            <div class="college_list_box">
              <div>
                <img
                  class="college_img"
                  :src="item.tpic"
                >
              </div>
              <div class="college_info">
                <p
                  v-if="item.type === 1"
                  class="college_list_box_p van-multi-ellipsis--l2 color1 fontBold content1"
                >
                  <van-tag
                    style="height:18px;width:55px;justify-content: center;"
                    class="tagleft"
                    color="#EE762A"
                    type="primary"
                  >
                    预告
                  </van-tag>
                  {{ item.title }}
                </p>
                <p
                  v-if="item.type === 2"
                  class="college_list_box_p van-multi-ellipsis--l2 color1 fontBold content1"
                >
                  <van-tag
                    style="height:18px;width:55px;justify-content: center;"
                    class="tagleft"
                    color="#F44A3D"
                    type="primary"
                  >
                    <img
                      class="radio"
                      src="@/assets/img/radio.gif"
                    >直播中
                  </van-tag>
                  {{ item.title }}
                </p>
                <p
                  v-if="item.type === 3"
                  class="college_list_box_p van-multi-ellipsis--l2 color1 fontBold content1"
                >
                  <van-tag
                    style="height:18px;width:55px;justify-content: center;"
                    class="tagleft"
                    color="#2A90EF"
                    type="primary"
                  >
                    回放
                  </van-tag>
                  {{ item.title }}
                </p>
                <p
                  v-if="item.type === 4"
                  class="college_list_box_p van-multi-ellipsis--l2 color1 fontBold content1"
                >
                  <van-tag
                    style="height:18px;width:55px;justify-content: center;"
                    class="tagleft"
                    color="#2A90EF"
                    type="primary"
                  >
                    <img
                      class="radio"
                      src="@/assets/img/radio.gif"
                    >录播
                  </van-tag>
                  {{ item.title }}
                </p>
                <div class="content2 font400 color6">
                  <span>直播时间:{{ item.start_time }}</span>
                  <!-- <span class=" left12">限时免费</span> -->
                </div>
              </div>
            </div>
          </div>
        </van-list>
      </van-pull-refresh>
    </div>
    <no-data v-if="demandList.length === 0" />
  </div>
  <div class="fexd_footer">
    <div class="footer_btn flex align_center justify_between">
      <div class="flex align_center">
        <div
          @click="back"
          class="agree font400 content3 color4"
        >
          返回
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, defineAsyncComponent } from 'vue'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
import { getmycourse } from '@/api/index'
const NoData = defineAsyncComponent(() => import('@/components/NoData/index.vue'))
const router = useRouter()
const store = useStore()
const loading: any = ref(false)
const finished: any = ref(false)
const refreshing: any = ref(false)
const form: any = ref({
  page: 1,
  pageSize: 10
})
const demandList: any = ref([])

const back = () => {
  if (store.state.from.path !== '/') {
    router.back()
  } else {
    router.replace('/')
  }
}

const onRefresh = async () => {
  form.value.page = 1
  const list:any = await loadDemandList()
  demandList.value = list
  refreshing.value = false
}

const loadMore = async () => {
  form.value.page = form.value.page + 1
  const list:any = await loadDemandList()
  demandList.value = [...demandList.value, ...list]
  loading.value = false
}

const loadDemandList = async () => {
  // 异步更新数据
  const res:any =
    (await getmycourse({ ...form.value })) || []
  if (!Array.isArray(res) && typeof res.list === 'object') {
    const keys:any = Object.keys(res.list)
    const newRes:any = keys.map((k:any) => res.list[k])
    finished.value = newRes.length < form.value.pageSize
    return newRes
  }

  finished.value = res.list.length < form.value.pageSize
  return res
}

onRefresh()

const goCurriculum = async (id: any) => {
  router.push({ path: '/find/curriculum', query: { id: id } })
}

</script>

<style lang="less" scoped>
.mycourse {
    padding: 0 15px 80px;
    margin-top: 40px;
    .mycourseBox{
        margin-top:15px;
    }
}

.college_list {
  width: 100%;
  margin: 15px auto;
  background-color: #fff;
  border-radius: 5px;
  .college_list_box {
    display: flex;
    padding: 15px;
    .college_info {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      .radio {
        width: 10px;
        height: 10px;
        margin-right: 5px;
      }
    }
    .college_list_box_p {
      line-height: 20px;
    }
    // justify-content: space-between;
    .college_img {
      width: 100px;
      height: 70px;
      border-radius: 5px;
      object-fit: cover;
      margin-right: 14px;
    }
    .left12 {
      margin-left: 10px;
      color:#EE762A;
    }
  }
}
.fexd_footer {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 70px;
    background-color: #fff;
    box-shadow: 0px 2px 12px 2px rgba(22, 12, 6, 0.09);
    .footer_btn {
        padding: 0 30px;
        height: 100%;
        text-align: center;
        .agree {
            width: 315px;
            height: 44px;
            background: #85898C;
            border-radius: 5px;
            text-align: center;
            line-height: 44px;
        }
    }
}
@media (min-width: 750px) {
  .mycourse {
    padding: 0 15px 80px;
    margin-top: 40px;
    .mycourseBox{
        margin-top:15px;
    }
}

.college_list {
  width: 100%;
  margin: 15px auto;
  background-color: #fff;
  border-radius: 5px;
  .college_list_box {
    display: flex;
    padding: 15px;
    .college_info {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      .radio {
        width: 10px;
        height: 10px;
        margin-right: 5px;
      }
    }
    .college_list_box_p {
      line-height: 20px;
    }
    // justify-content: space-between;
    .college_img {
      width: 100px;
      height: 70px;
      border-radius: 5px;
      object-fit: cover;
      margin-right: 14px;
    }
    .left12 {
      margin-left: 10px;
      color:#EE762A;
    }
  }
}
.fexd_footer {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 70px;
    background-color: #fff;
    box-shadow: 0px 2px 12px 2px rgba(22, 12, 6, 0.09);
    .footer_btn {
        padding: 0 30px;
        height: 100%;
        text-align: center;
        .agree {
            width: 315px;
            height: 44px;
            background: #85898C;
            border-radius: 5px;
            text-align: center;
            line-height: 44px;
        }
    }
}
}
</style>
